package top.maochang.hou.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import top.maochang.hou.dao.RoleDao;
import top.maochang.hou.dao.impl.RoleDaoImpl;
import top.maochang.hou.entity.SysRole;
import top.maochang.hou.entity.SysUser;
import top.maochang.hou.tools.UserContent;
import top.maochang.hou.tools.WebUtlis;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "role", value = "/role/*")
public class RoleServlet extends BaseServlet{

    /**
     * 分配角色（角色数据初始化）
     * @param req
     * @param resp
     * @throws IOException
     * @throws ServletException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * path /role/getRoleList
     */
    public void  getRoleList(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, InvocationTargetException, IllegalAccessException, SQLException {
        //获取左侧穿梭框数据
        RoleDao roleDao=new RoleDaoImpl();
        //得到角色数据
        List<SysRole> roleList = roleDao.getRoleList();
        //得到已经具备的角色信息
        SysUser user = (SysUser) UserContent.getUser();
        List<SysRole> roleListrByUserId = roleDao.getRoleListrByUserId(user.getId().toString());
        HashMap hashMap=new HashMap();
        hashMap.put("LeftRoles",roleList);
        hashMap.put("RightRoles",roleListrByUserId);
        ObjectMapper objectMapper = WebUtlis.getObjectMapper();
        String s = objectMapper.writeValueAsString(hashMap);
        resp.getWriter().write(s);
    }
}

